<template>
  <div v-loading="delLoading">
    <Search :search="search" :reset="reset">
      <template v-slot:body>
        <el-form-item label="生产汇报单号" prop="docno">
          <el-input v-model="query.docno" name="docno" clearable />
        </el-form-item>
        <el-form-item label="生产工单号" prop="production_no">
          <el-input v-model="query.production_no" name="production_no" clearable />
        </el-form-item>
      </template>
    </Search>
    <div class="table-default">
      <!-- <Operate :show="open" /> -->
      <el-table :data="tableData" class="mt-3" v-loading="loading" border>
        <el-table-column prop="docno" label="生产汇报单号" min-width="180" />
        <el-table-column prop="production_no" label="生产工单编号" min-width="180" />
        <el-table-column prop="type" label="汇报类型" min-width="110">
          <template #default="scope">
            <el-tag class="ml-2" type="warning" v-if="scope.row.type == 2">{{ typeFormatter[scope.row.type] }}</el-tag>
            <el-tag class="ml-2" type="success" v-if="scope.row.type == 1">{{ typeFormatter[scope.row.type] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="kingdee_status" label="单据状态" :formatter="formatter" min-width="110">
          <template #default="scope">
            <el-tag class="ml-2" type="warning" v-if="scope.row.kingdee_status == 0">{{ statusFormatter[scope.row.kingdee_status] }}</el-tag>
            <el-tag class="ml-2" type="success" v-if="scope.row.kingdee_status == 1">{{ statusFormatter[scope.row.kingdee_status] }}</el-tag>
            <el-tag class="ml-2" type="danger" v-if="scope.row.kingdee_status == 2">{{ statusFormatter[scope.row.kingdee_status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" min-width="150" />
        <el-table-column prop="creator" label="创建人" min-width="130" />
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <!-- <Update @click="open(scope.row.id)" />-->
            <router-link :to="{ path: '/production/ReportDetail', query: { id: `${scope.row.id}` } }">
              <Show />
            </router-link>
            <Destroy @click="destroy(api, scope.row.id)" />

            <!-- <Destroy @click="delHandler(scope.row.id)"  /> -->
          </template>
        </el-table-column>
      </el-table>
      <Paginate />
    </div>

    <Dialog v-model="visible" :title="title" destroy-on-close>
      <Create @close="close(reset)" :primary="id" :api="api" />
    </Dialog>

    <!-- 确认关闭弹窗 -->
    <!-- <el-dialog v-model="dialogVisible" title="关闭提示" width="30%">
      <span>确认删除该工单吗？</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">关闭</el-button>
          <el-button type="primary" @click="deleteHandler"> 确认 </el-button>
        </span>
      </template>
    </el-dialog> -->
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue'
import Create from './create.vue'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import { setTableHeight } from '/admin/utils/setHeight.js'
import http from '/admin/support/http'
import { ElMessageBox, ElMessage } from 'element-plus'

const api = 'production/report/order'

const { data, query, search, reset, loading } = useGetList(api)
const { destroy, deleted, delLoading } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data)

const dialogVisible = ref(false)

const delId = ref(null)

// 删除
const delHandler = id => {
  console.log('点击的id', id)
  delId.value = id
  dialogVisible.value = true
}

const deleteHandler = () => {
  dialogVisible.value = false
  console.log('删除的id', delId.value)
  http.delete(`/production/report/order/${delId.value}`).then(r => {
    let res = r.data
    console.log('删除请求', res)
    if (res.code == 10000) {
      ElMessage.success(res.message)
      search()
    } else {
      ElMessage.error(res.message)
    }
  })
}

const statusFormatter = {
  0: '待推送',
  1: '推送成功',
  2: '推送失败',
}
const typeFormatter = {
  1: '成品',
  2: '半成品',
}

const formatter = (row, column, value, index) => {
  return {
    0: '待推送',
    1: '推送成功',
    2: '推送失败',
  }[value]
}

onMounted(() => {
  search()
  deleted(reset)
  setTableHeight('.table-default')
})
</script>
<style lang="css">
.el-table {
  height: 87%;
}
.el-button--small {
  margin-right: 10px;
}
</style>